package com.zzhrtech.wgzx_cityzen.ui.task;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.GsonRequest;
import com.zzhrtech.WgzxApplication;
import com.zzhrtech.wgzx_cityzen.BaseActivity;
import com.zzhrtech.wgzx_cityzen.Constans;
import com.zzhrtech.wgzx_cityzen.R;
import com.zzhrtech.wgzx_cityzen.adapters.task.TaskFilterAdapter;
import com.zzhrtech.wgzx_cityzen.beans.disease.AreaListBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class
TaskFilterActivity extends BaseActivity {

//    RecyclerView recy_qudao,recy_quyu;
    TaskFilterAdapter filterAdapter;
    String[] qudaos = new String[]{"内部员工","电话和微博","应急突发险情","重大安全隐患","市民"};
    String[] qudaoIds = new String[]{"0","1","2","3","5"};
    RadioGroup group_quyu,group_qudao;

    Button btn_cancle,btn_sure;
    private String areaId = "",qudaoId = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_filter);

        toolbar.setNavigationIcon(R.mipmap.ic_close);

        group_quyu = (RadioGroup) findViewById(R.id.group_quyu);
        group_qudao = (RadioGroup) findViewById(R.id.group_qudao);

        btn_cancle = (Button) findViewById(R.id.btn_cancle);

        btn_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn_sure = (Button) findViewById(R.id.btn_sure);

        btn_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (areaId.isEmpty() & qudaoId.isEmpty()){
                    showToast("请选择信息");
                }else {
                    Intent intent = new Intent();
                    intent.putExtra("areaid", areaId);
                    intent.putExtra("qudaoid", qudaoId);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });

        httpGetArea();
    }


    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {

            if (msg.what == 0){

                final List<AreaListBean.Data> areaData = (List<AreaListBean.Data>) msg.obj;

                RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.setMargins(16,8,16,8);
                for (int i = 0;i< areaData.size();i++) {
                    RadioButton button = new RadioButton(TaskFilterActivity.this);
                    button.setPadding(16,8,16,8);
                    button.setLayoutParams(params);
                    button.setText(areaData.get(i).getA_name());


                    final int finalI = i;
                    button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            if (isChecked){
                                areaId = areaData.get(finalI).getA_id();
                            }

                        }
                    });

                    group_quyu.addView(button);
                }

                for (int i = 0;i< qudaos.length;i++){
                    RadioButton button = new RadioButton(TaskFilterActivity.this);
                    button.setPadding(16,8,16,8);
                    button.setLayoutParams(params);
                    button.setText(qudaos[i]);

                    final int finalI = i;
                    button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            if (isChecked){
                                qudaoId = qudaoIds[finalI];
                            }
                        }
                    });

                    group_qudao.addView(button);
                }

            }

            return false;
        }
    });

    private void httpGetArea(){

        Map<String,String> params = new HashMap<String, String>();
//        params.put("dep_id",getCache().getAsString(Constans.keyDepid));

        GsonRequest<AreaListBean> gsonRequest = new GsonRequest<AreaListBean>(com.android.volley.Request.Method.POST, Constans.HTTP_AREALIST, AreaListBean.class, params, new com.android.volley.Response.Listener<AreaListBean>() {
            @Override
            public void onResponse(AreaListBean response) {

                if (response.isSuccess()){
                    Message message = new Message();
                    message.what = 0;
                    message.obj = response.getData();
                    mHandler.sendMessage(message);
                }else {
                    showToast(response.getMsg());
//                    mDialog.dismiss();
                }
            }
        }, new com.android.volley.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("result=====",error + "");
//                mDialog.dismiss();
            }
        });

        WgzxApplication.addRequest(gsonRequest, Constans.reqTag);
    }
}
